package sfsoftware.game.actor.enemy;

import sfsoftware.util.Clock;
import sfsoftware.util.Sound;
import sfsoftware.util.Tile;

public class ScubaShooter extends SimpleShooter 
{
	private int scubaShooterState = 0;
	protected Clock animationTimer = new Clock(60);	
	
	public ScubaShooter(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) 
	{
		super(destX, destY, texture, destWidth, destHeight, assocTileMap);
		setSrcY(72);
	}

	public ScubaShooter(int destX, int destY, String texture, int srcX, int srcY, int destWidth, int destHeight, Tile[][] assocTileMap) 
	{
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight, assocTileMap);
		setSrcY(72);
	}
	
	/**
	 * 	Logic for scuba shooter enemy: 
	 * 	1. Randomly "surface" about water
	 * 	2. Randomly decide to shoot
	 * 	3. Retreat after shooting 
	 * 	4. Repeat.
	 */
	@Override
	public void update() {	
		
		if(!(isInvulnerable()))
		{			
			switch(scubaShooterState)
			{
				case 0:
					// Waiting, lurking... under the water
					if(((int)(Math.random() * 300)) == 0)
					{
						scubaShooterState = 1;
					}
					else if(!firingBullet)	// Theoretically, firingBullet should always be false in this state.
					{
						switch(direction)
						{
							case 0:
								move(0, 1, tileMap);
								break;
							case 1:
								move(0, -1, tileMap);
								break;
							case 2:				
								move(1, 0, tileMap);
								break;
							case 3:
								move(-1, 0, tileMap);
								break;				
						}
					}
					break;
				case 1:
					// Rising from the depths!
					if(getSrcX() >= 80)
					{
						scubaShooterState = 2;
					}
					break;
				case 2:
					// Waiting to randomly shoot...
					if(!firingBullet)
					{
						int shootOpportunity = ((int)(Math.random() * 75)) * shootFrequency;
						if(shootOpportunity == 0)
						{
							firingBullet = true;
						}
					}
					break;
				case 3:
					// Descending into the depths...
					if(getSrcX() <= 0)
					{
						scubaShooterState = 0;
					}
					break;
				case 4:
					// TODO I like the idea of this "cooldown" state. Maybe have some bubbles appear until we reset to state 0
					break;
			}
		}

		if(firingCountdownClock.updateClock())
		{
			if(firingBullet)
			{
				firingCountdown++;
				if(firingCountdown > 4)
				{
					// Here is where we create a new smart bullet! We use setCollidable to allow this bullet to pass through non-walkable tiles
					(new SmartBullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap, 15)).setCollidable(false);					
					Sound.playSound("shooter");
					firingCountdown = 0;
					firingBullet = false;
					// Change state so the enemy dives down again
					scubaShooterState = 3;
				}
			}
		}		
		
		if(animationTimer.updateClock())
		{
			// Rising animation
			if(scubaShooterState == 1 && getSrcX() < 80)
			{
				setSrcX(getSrcX() + 20);
			}
			// Diving animation
			else if(scubaShooterState == 3 && getSrcX() > 0)
			{
				setSrcX(getSrcX() - 20);
			}
		}
	}
		
	
}
